package com.springboot2.service.Impl;

import com.springboot2.entity.Cars;
import com.springboot2.entity.Goods;
import com.springboot2.mapper.CarMapper;
import com.springboot2.mapper.GoodsMapper;
import com.springboot2.service.CarsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class CarsServiceImpl implements CarsService {

    @Resource
    private CarMapper carMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public void add(Cars cars, int userId, int goodsId) {
        Date date = new Date();
        cars.setGoodsId(goodsId);
        cars.setCreateTime(date);
        cars.setIsDel(1);
        cars.setUserId(userId);
        System.out.println(cars);
        carMapper.add(cars);
    }

    @Override
    public void del(int id) {
        carMapper.del(id);
    }

    @Override
    public void update(Cars cars) {
        Date date = new Date();
        cars.setCreateTime(date);
        cars.setIsDel(1);
        carMapper.update(cars);
    }

    @Override
    public Cars findById(int id) {
        return carMapper.findById(id);
    }

    @Override
    public List<Cars> findByPage(int pageNo, int pageSize) {
        Map<String ,Integer> map = new HashMap<>();
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        return carMapper.findByPage(map);
    }

    @Override
    public int count() {
        return carMapper.count();
    }

    @Override
    public List<Cars> findAll(int userId) {
        List<Cars> lists =  carMapper.findAll(userId);
        for (Cars list: lists){
            Goods goods = goodsMapper.findById(list.getGoodsId());
            list.setGoods(goods);
        }
        return lists;
    }
}
